package com.clear.honghefinance.admin.controller;

import com.clear.honghefinance.biz.modules.system.member.domain.dto.*;
import com.clear.honghefinance.biz.modules.system.member.service.MemberLoginService;
import com.clear.honghefinance.common.dto.entity.ApiResponse;
import com.clear.honghefinance.common.dto.vo.TokenResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 登录controller
 * </p>
 */
@Api(tags = "用户登录模块")
@RestController
@RequestMapping("/login")
@Slf4j
@RequiredArgsConstructor
public class LoginController {

    private final MemberLoginService memberLoginService;

    /**
     * 获取客户端id 用于标识客户端的身份（未登录前）
     *
     * @return clientId
     */
    @ApiOperation("获取客户端Id")
    @GetMapping(value = "/getClientId")
    public ApiResponse<String> getClientId() {
        String clientId = memberLoginService.getClientId();
        return ApiResponse.success(clientId);
    }


    /**
     * 获取图形验证码
     *
     * @param getBase64CodeDTO req
     * @return 图形验证码String
     */
    @ApiOperation(value = "获取图形验证码")
    @GetMapping(value = "/getBase64Code")
    public ApiResponse<String> getBase64Code(@Validated GetBase64CodeDTO getBase64CodeDTO) {
        String base64Code = memberLoginService.getBase64Code(getBase64CodeDTO);
        return ApiResponse.success(base64Code);
    }

    /**
     * 获取短信验证码
     *
     * @return void
     */
    @ApiOperation(value = "获取短信验证码")
    @GetMapping(value = "/sendSmsCode")
    public ApiResponse<Void> sendSmsCode(@Validated GetSmsCodeDTO getUserSmsCodeDTO) {
        memberLoginService.sendSmsCode(getUserSmsCodeDTO);
        return ApiResponse.success();
    }

    /**
     * 手机号密码登录
     *
     * @param dto req
     * @return 返回用户token信息
     */
    @ApiOperation(value = "手机号密码登录")
    @PostMapping(value = "/phonePasswordLogin")
    public ApiResponse<TokenResponse> phonePasswordLogin(@Validated @RequestBody PhonePasswordLoginDTO dto) {
        TokenResponse tokenResponse = memberLoginService.phonePasswordLogin(dto);
        return ApiResponse.success(tokenResponse);
    }

    /**
     * 手机短信验证码登录
     *
     * @param dto req
     * @return 返回用户token信息
     */
    @ApiOperation(value = "手机短信验证码登录")
    @PostMapping(value = "/phoneSmsCodeLogin")
    public ApiResponse<TokenResponse> phoneSmsCodeLogin(@Validated @RequestBody PhoneSmsCodeLoginDTO dto) {
        TokenResponse tokenResponse = memberLoginService.phoneSmsCodeLogin(dto);
        return ApiResponse.success(tokenResponse);
    }

    /**
     * 获取客户端token信息（轮询接口，检测用户登录状态）
     *
     * @param dto req
     * @return 返回用户token信息
     */
    @ApiOperation(value = "获取客户端token信息")
    @GetMapping(value = "/getClientToken")
    public ApiResponse<TokenResponse> getClientToken(@Validated @ModelAttribute GetClientTokenDTO dto) {
        TokenResponse result = memberLoginService.getClientToken(dto.getClientId());
        return ApiResponse.success(result);
    }
}
